package asteroids.entity.equipment;

import java.awt.Graphics;

import org.apache.log4j.Logger;

import asteroids.engine.GameState;
import asteroids.engine.Utils;
import asteroids.entity.GameObject;
import asteroids.entity.Ship;

public abstract class Equipment extends GameObject
{
    private Logger logger = Logger.getLogger(Equipment.class);
    protected Ship ship;
    protected boolean updateAfterDeath;

    public Equipment(Ship ship, Utils.PolarCoordinate[] shape)
    {
        super(ship.getXPosition(), ship.getYPosition(), shape, GameObject.ObjectType.ICING);
        this.ship = ship;
        drawMe = false;
        updateAfterDeath = false;
    }

    public Equipment(Ship ship)
    {
        this(ship, Utils.POINT_SHAPE);
    }

    public Ship getOwner()
    {
        return ship;
    }

    public boolean isAlive()
    {
        return ship.isAlive() || (ship.isActive() && updateAfterDeath);
    }

    /**
     * Allows the Equipment to use GameState information
     * 
     * @param state
     */
    public void update(GameState state)
    {
        // do cool things, in subclasses anyway
        super.update();
    }

    public void update()
    {
        logger.warn("Calling update() on equipment instead of update(GameState)");
        super.update();
    }

    public boolean updateAfterDeath()
    {
        return updateAfterDeath;
    }

    /**
     * Called when ship respawns
     */
    public void respawn()
    {
        // override in subclasses if needed
    }

    public void draw(Graphics g)
    {
        if( ship.isAlive() || updateAfterDeath )
        {
            logger.trace("drawing equipment");
            // granted, drawMe is set to false by default, but for those things
            // which do get drawn, we still want that to happen only when the
            // ship is alive
            super.draw(g);
        }
    }
}